package auth

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"gitee.com/ProjectManageSys/model"
)

func Login(c *gin.Context) {
	var loginData struct {
		Name     string `form:"username"`
		Password string `form:"password"`
	}
	if err := c.ShouldBind(&loginData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var user = model.User{}
	model.DB.Where("Name = ?", loginData.Name).First(&user)
	if user.Name == loginData.Name && user.Password == loginData.Password {
		c.IndentedJSON(http.StatusOK, gin.H{"name": loginData.Name,
			"password": loginData.Password})
		return
	}
	c.IndentedJSON(http.StatusOK, gin.H{"message": "user not found"})
}
